package com.jdh.fileUpload.service.fileUpload;

import com.alibaba.fastjson2.JSONObject;
import com.jdh.fileUpload.config.MinioUtil;
import com.jdh.fileUpload.utils.Result;
import com.jdh.fileUpload.utils.Uuid;
import com.jdh.fileUpload.vo.PartFileIndexVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @ClassName: FileUploadMinioServiceImpl
 * @Author: jdh
 * @CreateTime: 2022-03-15
 * @Description:
 */
@Slf4j
@Service
@ConditionalOnProperty(prefix = "autoconfigure", value = "isMinio", matchIfMissing = false)
public class FileUploadMinioServiceImpl implements FileUploadMinioService {

    @Autowired
    private MinioUtil minioUtil;

    @Resource
    private RedissonClient redissonClient;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result<Boolean> fileUploadMinio(MultipartFile file) {
        String objectName = Uuid.generateUniqueId(8) + "_" + file.getOriginalFilename();

        try {
            Boolean aBoolean = minioUtil.uploadFile(file, "minio-file", objectName);
            log.info("文件上传是否完成：{}", aBoolean);
            return Result.success(aBoolean, "文件上传完成！");
        } catch (Exception e) {
            log.info("文件上传异常！", e);
            return Result.error(false, "文件上传异常！");
        }
    }

    @Override
    public Result<Boolean> filePartUploadMinio(MultipartFile file, String partFile) {

//        String objectName = Uuid.generateUniqueId(8) + "_" + file.getOriginalFilename();
        try {
            PartFileIndexVo partFileIndexVo = JSONObject.parseObject(partFile, PartFileIndexVo.class);
            String objectName = "temp/" + partFileIndexVo.getFileUid() + "/"
                    + partFileIndexVo.getFileName().substring(0, partFileIndexVo.getFileName().lastIndexOf(".")) + "_"
                    + partFileIndexVo.getPartIndex() + ".temp";
            //上传文件分片
            Boolean partState = minioUtil.uploadFile(file, "minio-file", objectName);

            if (partState) {

                Long result = redisTemplate.opsForValue().increment("partCount:" + partFileIndexVo.getFileUid());

                if (result.equals(partFileIndexVo.getPartTotalNum().longValue())) {

                    log.info("{}:开始合并分片请求...", partFileIndexVo.getFileName());
                    //开始合并分片
                    Boolean complete = minioUtil.uploadFileComplete("minio-file", "temp/" + partFileIndexVo.getFileUid(),
                            partFileIndexVo.getFileName(), partFileIndexVo.getPartTotalNum());
                    //移除文件分片上传情况，这个也应该在redis中
                    Boolean delete = false;
                    if (complete) {
                        delete = redisTemplate.delete("partCount:" + partFileIndexVo.getFileUid());
                        log.info("{}:分片文件合并完成！", partFileIndexVo.getFileName());
                    }
                    return Result.success(complete && delete, partFileIndexVo.getFileUid());
                }
            }
            return Result.success(partState, partFileIndexVo.getPartIndex().toString());
        } catch (Exception e) {
            log.info("文件上传异常！", e);
            return Result.error("文件上传异常！");
        }
    }

}
